package com.itheima.springbootrequestresponse.controller;

import com.itheima.springbootrequestresponse.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
public class RequestController {
    /**
     * 简单参数1
     * @param request
     * @return
     */
    @RequestMapping("/simpleParam1")
    public String simpleParam1(HttpServletRequest request){
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        Integer ageInt = Integer.parseInt(age);
        System.out.println("参数是: name =" +name +"age" + ageInt );
        return "简单参数1";
    }

    /**
     * 简单参数2
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/simpleParam2")
    public String simpleParam2(String name , Integer age){
        System.out.println("参数是 name :" +name +"and, age: " +age);
        return "简单参数2";
    }

    /**
     * 简单参数3
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/simpleParam3")
    public String simpleParam3(@RequestParam(name ="name1") String name , Integer age){
        System.out.println("参数是 name :" +name +"and, age: " +age);
        return "简单参数3";
    }

    /**
     * 实体参数
     * @param user
     * @return
     */
    @RequestMapping("/pojoParam1")
    public String pojoParam1(User user){
        System.out.println("实体参数是 name :" +user.getName() +" age: " +user.getAge());
        return "实体参数1";
    }

    /**
     * 实体参数-嵌套参数
     * @param user
     * @return
     */
    @RequestMapping("/pojoParam2")
    public String pojoParam2(User user){
        System.out.println("实体参数是 :"+ user);
        return "实体参数2";
    }

    @RequestMapping("/arrParam1")
    public String arrParam1(String[] hobby){
        System.out.println("数组参数是 :"+ Arrays.toString(hobby));
        return "数组参数1";
    }

    /**
     * 集合参数  +@RequestParam
     * @param hobby
     * @return
     */

    @RequestMapping("/listparam1")
    public String listparam1(@RequestParam List<String> hobby){
        System.out.println("集合参数是 :" + hobby);
        return "集合参数1";
    }

    /**
     * 时间参数 + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss" )
     * @param brithday
     * @return
     */

    @RequestMapping("/dateParam1")
    public String dateParam1(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss" ) LocalDateTime brithday){
        System.out.println("时间参数是 :" + brithday);
        return "时间参数1";
    }

    /**
     * JSON参数  @RequestBody 直接用实体(对象)接收
     * @param user
     * @return
     */
    @RequestMapping("/jsonParam1")
    public String jsonParam1(@RequestBody User user){
        System.out.println("json参数是 :" + user);
        return "json参数1";
    }

    /**
     * 路径参数 + @PathVariable
     * @param id
     * @return
     */

    @RequestMapping("/pathParam1/{id}")
    public String pathParam1(@PathVariable  String id){
        System.out.println("路径参数是 :" + id);
        return "路径参数1";
    }



}
